import React from 'react'
import styles from './Pagination.css'
import { Pagination, Input } from 'antd'
import R from 'ramda'

type Props = {
  pageSize: number,
  total: number,
  current: number,
  onChange: Function
}
type State = {
  current: number,
  pageSize: number
}
class PaginationPage extends React.Component {
  props: Props
  state: State
  pageChange: Function
  onShowSizeChange: Function
  constructor (props: Props) {
    super(props)
    this.state = {
      current: this.props.current,
      pageSize: this.props.pageSize
    }
    this.pageChange = this.pageChange.bind(this)
    this.onShowSizeChange = this.onShowSizeChange.bind(this)
  }
  componentWillReceiveProps (nextProps: Object) {
    if (!R.equals(nextProps, this.props)) {
      this.setState({
        current: nextProps.current,
        pageSize: nextProps.pageSize
      })
    }
  }

  pageChange (page: number, pageSize: number) {
    this.setState({
      current: page
    })
    this.props.onChange(page, pageSize)
  }
  onShowSizeChange (e) {
    const pageSize = e.target.value
    const { current } = this.state
    if (pageSize > 0) {
      this.setState({
        pageSize: pageSize,
        current: 1
      })
      this.props.onChange(current, pageSize)
    }
  }

  render () {
    const { total } = this.props
    const { current, pageSize } = this.state
    return (
      <div className={styles['pagination']}>
        <Input
          style={{ width: 40, marginRight: 5 }}
          min={1}
          defaultValue={pageSize}
          onPressEnter={this.onShowSizeChange} />
          条/页
        <Pagination
          current={current}
          pageSize={pageSize}
          total={total}
          showTotal={total => `共 ${total} 条数据`}
          showQuickJumper
          onChange={this.pageChange} />
      </div>
    )
  }
}
export default PaginationPage
